package com.example.applets.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.applets.entity.Carousel;
import com.example.applets.service.impl.CarouselServiceImpl;
import com.example.applets.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 轮播图 前端控制器
 * </p>
 *
 * @author 云烟成雨丶
 * @since 2020-10-09
 */
@Controller
@RequestMapping("/applets/carousel")
public class CarouselController {
    @Autowired
    private CarouselServiceImpl carouselService;

    @Value("${file.path}")
    private String paths;

    @RequestMapping("/page")
    @ResponseBody
    //分页
    public AjaxResult selectPage(@RequestParam("title") String title, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        Page<Carousel> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Carousel> wrapper = new QueryWrapper<>();
        if (!title.equals(""))
            wrapper.like("title", title);
        List<Carousel> list = carouselService.page(page, wrapper).getRecords();
        int num = (carouselService.count(wrapper) - 1) / pageSize + 1;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", num);
        if (list.size() == 0)
            return AjaxResult.error("暂无数据!");
        return AjaxResult.success(map);
    }

    @RequestMapping("/add")
    @ResponseBody
    //增加商品种类
    public AjaxResult addCarousel(@RequestParam(value = "urlpic", required = false) MultipartFile picture, Carousel carousel) throws IOException {
        if (!picture.isEmpty()) {
            //起新名字
            String oldName = picture.getOriginalFilename();
            String prefix = oldName.substring(oldName.lastIndexOf('.'));
            String newName = UUID.randomUUID() + prefix;
            //修改新名字
            carousel.setUrl(newName);
            //确定路径
            String path = paths;
            //修改存储路径
            picture.transferTo(new File(path, newName));
        }
        if (!carouselService.save(carousel))
            return AjaxResult.error("保存失败!");
        return AjaxResult.success("保存成功!");
    }

    @RequestMapping("/select")
    public String selectCarousel(@RequestParam("id") long id, Model model) {
        Carousel carousel = carouselService.getById(id);
        model.addAttribute("c", carousel);
        return "KCarousel/UpdateCarousel";
    }

    @RequestMapping("/update")
    @ResponseBody
    //修改
    public AjaxResult updateCarousel(@RequestParam(value = "urlpic", required = false) MultipartFile picture, Carousel carousel) throws IOException {
        if (!picture.isEmpty()) {
            //起新名字
            String oldName = picture.getOriginalFilename();
            String prefix = oldName.substring(oldName.lastIndexOf('.'));
            String newName = UUID.randomUUID() + prefix;
            //修改新名字
            carousel.setUrl(newName);
            //确定路径
            String path = paths;
            //修改存储路径
            picture.transferTo(new File(path, newName));
        }
        if (!carouselService.updateById(carousel))
            return AjaxResult.error("修改失败!");
        return AjaxResult.error("修改成功!");
    }

    @RequestMapping("/detele")
    @ResponseBody
    //删除
    public AjaxResult deteleCarousel(@RequestParam("carouselId") long id) {
        if (!carouselService.removeById(id))
            return AjaxResult.error("删除失败!");
        return AjaxResult.error("删除成功!");

    }
}

